﻿namespace Yipusoft.Account.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Yipusoft.Account.DBUtility;
    using Yipusoft.Account.Model;

    public class Menu
    {
        public bool Delete(string MenuID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = MenuID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Menus_Delete", commandParameters) > 0);
            }
        }

        public bool Exsits(string MenuID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = MenuID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Menus_Exsits", commandParameters).Read();
            }
        }

        public List<MenuInfo> GetList(string StrWhere)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            MenuInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.NVarChar, 0xfa0) };
            commandParameters[0].Value = StrWhere;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Menus_GetList", commandParameters);
                while (reader.Read())
                {
                    item = new MenuInfo();
                    item.MenuID = reader.GetString(0);
                    item.ParentID = reader.GetString(1);
                    item.MenuLevel = reader.GetInt32(2);
                    item.MenuName = reader.GetString(3);
                    item.MenuUrl = reader.GetString(4);
                    item.IsBlank = reader.GetBoolean(5);
                    item.MenuImage = reader.GetString(6);
                    item.SeqID = reader.GetInt32(7);
                    item.Description = reader.GetString(8);
                    list.Add(item);
                }
                return list;
            }
        }

        public MenuInfo GetMenu(string MenuID)
        {
            MenuInfo info = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = MenuID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Menus_GetDetail", commandParameters);
                if (reader.Read())
                {
                    info = new MenuInfo();
                    info.MenuID = reader.GetString(0);
                    info.ParentID = reader.GetString(1);
                    info.MenuLevel = reader.GetInt32(2);
                    info.MenuName = reader.GetString(3);
                    info.MenuUrl = reader.GetString(4);
                    info.IsBlank = reader.GetBoolean(5);
                    info.MenuImage = reader.GetString(6);
                    info.SeqID = reader.GetInt32(7);
                    info.Description = reader.GetString(8);
                }
                return info;
            }
        }

        public bool Insert(MenuInfo Menu)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.NVarChar, 50), new SqlParameter("@ParentID", SqlDbType.NVarChar, 50), new SqlParameter("@MenuLevel", SqlDbType.Int), new SqlParameter("@MenuName", SqlDbType.NVarChar, 0xff), new SqlParameter("@MenuUrl", SqlDbType.NVarChar, 0xff), new SqlParameter("@IsBlank", SqlDbType.Bit), new SqlParameter("@MenuImage", SqlDbType.NVarChar, 0xff), new SqlParameter("@SeqID", SqlDbType.Int), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = Menu.MenuID;
            commandParameters[1].Value = Menu.ParentID;
            commandParameters[2].Value = Menu.MenuLevel;
            commandParameters[3].Value = Menu.MenuName;
            commandParameters[4].Value = Menu.MenuUrl;
            commandParameters[5].Value = Menu.IsBlank;
            commandParameters[6].Value = Menu.MenuImage;
            commandParameters[7].Value = Menu.SeqID;
            commandParameters[8].Value = Menu.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Menus_Insert", commandParameters) > 0);
            }
        }

        public bool Update(MenuInfo Menu)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.NVarChar, 50), new SqlParameter("@ParentID", SqlDbType.NVarChar, 50), new SqlParameter("@MenuLevel", SqlDbType.Int), new SqlParameter("@MenuName", SqlDbType.NVarChar, 0xff), new SqlParameter("@MenuUrl", SqlDbType.NVarChar, 0xff), new SqlParameter("@IsBlank", SqlDbType.Bit), new SqlParameter("@MenuImage", SqlDbType.NVarChar, 0xff), new SqlParameter("@SeqID", SqlDbType.Int), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = Menu.MenuID;
            commandParameters[1].Value = Menu.ParentID;
            commandParameters[2].Value = Menu.MenuLevel;
            commandParameters[3].Value = Menu.MenuName;
            commandParameters[4].Value = Menu.MenuUrl;
            commandParameters[5].Value = Menu.IsBlank;
            commandParameters[6].Value = Menu.MenuImage;
            commandParameters[7].Value = Menu.SeqID;
            commandParameters[8].Value = Menu.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Menus_Update", commandParameters) > 0);
            }
        }
    }
}

